package com.tsunami.context;


import com.tsunami.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author by lx
 */
public class BaseContextHandler {
    private BaseContextHandler() {
    }

    private static final ThreadLocal<Map<String, String>> THREAD_LOCAL = new ThreadLocal<>();
    private static final String NULL_STRING = "null";

    public static void set(String key, Long value) {
        Map<String, String> map = getLocalMap();
        map.put(key, value == null ? "0" : String.valueOf(value));
    }

    public static void set(String key, String value) {
        Map<String, String> map = getLocalMap();
        map.put(key, value == null ? "" : value);
    }

    public static void set(String key, Boolean value) {
        Map<String, String> map = getLocalMap();
        map.put(key, value == null ? "false" : value.toString());
    }


    public static Map<String, String> getLocalMap() {
        Map<String, String> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new HashMap<>(10);
            THREAD_LOCAL.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, String> threadLocalMap) {
        THREAD_LOCAL.set(threadLocalMap);
    }


    public static String get(String key) {
        Map<String, String> map = getLocalMap();
        return map.getOrDefault(key, "");
    }

    /**
     * 账号id
     *
     * @return 用户ID
     */
    public static Long getUserId() {
        Object value = get(BaseContextConstants.JWT_KEY_USER_ID);
        if ("".equals(value.toString())) {
            return 0L;
        }
        return Long.valueOf(value.toString());
    }

    /**
     * 账号id
     *
     * @param userId 登录用户ID
     */
    public static void setUserId(Long userId) {
        if (userId != null) {
            set(BaseContextConstants.JWT_KEY_USER_ID, userId);
        }

    }

    public static void setUserId(String userId) {
        if (StringUtils.isNotEmpty(userId) && !NULL_STRING.equals(userId)) {
            setUserId(Long.valueOf(userId));
        }
    }

    /**
     * 客户id
     *
     * @return 租户ID
     */
    public static Long getTenantId() {
        Object value = get(BaseContextConstants.JWT_KEY_TENANT_ID);
        if (value == "" || "".equals(value)) {
            return 0L;
        } else {
            return Long.valueOf(value.toString());
        }

    }

    /**
     * 客户id
     *
     * @param tenantId 租户ID
     */
    public static void setTenantId(String tenantId) {
        set(BaseContextConstants.JWT_KEY_TENANT_ID, tenantId);
    }

    /**
     * 账号表中的name
     *
     * @return 账号
     */
    public static String getAccount() {
        Object value = get(BaseContextConstants.JWT_KEY_ACCOUNT);
        return returnObjectValue(value);
    }

    /**
     * 账号表中的name
     *
     * @param name 账号
     */
    public static void setAccount(String name) {
        set(BaseContextConstants.JWT_KEY_ACCOUNT, name);
    }

    /**
     * 登录的账号
     *
     * @return 用户名
     */
    public static String getName() {
        Object value = get(BaseContextConstants.JWT_KEY_NAME);
        return returnObjectValue(value);
    }

    /**
     * 用户名称
     *
     * @param account 用户名称
     */
    public static void setName(String account) {
        set(BaseContextConstants.JWT_KEY_NAME, account);
    }

    /**
     * 获取用户token
     *
     * @return 获取用户token
     */
    public static String getToken() {
        Object value = get(BaseContextConstants.TOKEN_NAME);
        return value == null ? "" : value.toString();
    }

    public static void setToken(String token) {
        set(BaseContextConstants.TOKEN_NAME, token);
    }

    public static Long getOrgId() {
        Object value = get(BaseContextConstants.JWT_KEY_ORG_ID);
        return value == null || value == "" ? 0L : Long.parseLong(value.toString());
    }

    public static void setOrgId(String val) {
        set(BaseContextConstants.JWT_KEY_ORG_ID, val);
    }


    public static Long getStationId() {
        Object value = get(BaseContextConstants.JWT_KEY_STATION_ID);
        return value == null || value == "" ? 0L : Long.parseLong(value.toString());
    }

    public static void setStationId(String val) {
        set(BaseContextConstants.JWT_KEY_STATION_ID, val);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? "" : value.toString();
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
